package com.lvyq.myplayer.ui.fragment.ranking

import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.ranking.TheChartAdapter
import com.lvyq.myplayer.base.BaseFragment
import com.lvyq.myplayer.model.ranking.RankingBean
import com.lvyq.myplayer.model.ranking.RankingInfo
import com.lvyq.myplayer.model.ranking.RankingItemModel
import com.lvyq.myplayer.net.api.indexDailyPlayApiService
import com.lvyq.myplayer.ui.activity.ranking.TheCharItemActivity
import com.lvyq.myplayer.util.NetworkUtils
import com.lvyq.myplayer.util.URLProviderUtils

/**
 * 文件描述：TheChartFragment
 * 作者：lvyq
 * 创建时间：2025-05-29
 * 修改时间：2025-05-29
 */
class TheChartFragment: BaseFragment() {

    lateinit var rankingRecyclerView:RecyclerView

    override fun initView(): View? {
        var view = View.inflate(context, R.layout.fragment_ranking, null)
        rankingRecyclerView=view.findViewById(R.id.rankingRecyclerView)

        //使用LingerLayout 宽度match_parent异常，这里暂时用GridLayout
        rankingRecyclerView.layoutManager = GridLayoutManager(context,1)
        return view
    }

    override fun initData() {
        var type = arguments?.getInt("RANKING_TYPE")

        var theChartAdapter=TheChartAdapter(context)
        val url = URLProviderUtils.getRankingList(type)
        NetworkUtils.get(
            url = url,
            responseClass = RankingBean::class.java,
            successCallback = { pageData ->
                theChartAdapter.initData(
                    ArrayList(pageData.rows)
                )
            },
            errorCallback = { errorMsg ->
                Toast.makeText(context, "加载失败：$errorMsg", Toast.LENGTH_SHORT).show()
                Log.e("TheChartFragment", "加载失败：$errorMsg")
            }
        )

        rankingRecyclerView.adapter = theChartAdapter

        theChartAdapter.setOnItemClickListener(object :TheChartAdapter.OnItemClickListener{
            override fun onItemClick(data: RankingInfo) {
                var intent=Intent(context,TheCharItemActivity::class.java)
                intent.putExtra("rankingId",data.id)
                //背景图
                intent.putExtra("bannerImg",data.bgImg)
                startActivity(intent)
            }


        })

    }




}